import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/1/31
 * Time:19:50
 **/
public class TestSort {
    /**
     * 堆排，归并：不管好坏 都是:O(nlogn)
     * 快排：最坏:O(n^2)
     * k*n*logn
     */

    /**
     * 归并排序
     * 时间复杂度：O(nlogn)
     * 空间复杂度：O(n)
     * 稳定性：稳定
     * @param array
     * @param low
     * @param high
     */
    public static void mergeSortInternal(int[] array,int low,int high){
        if(low >= high) return;
        int mid = (low+high)/2;
        mergeSortInternal(array,low,mid);//左
        mergeSortInternal(array,mid+1,high);//右
        //合并
        merge(array,low,mid,high);
    }
    public static void merge(int[] array,int low,int mid,int high){
        /*int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        while(s1<=e1&&s2<=e2){

        }*/
        int s1 = low;
        int s2 = mid+1;
        int[] tmp = new int[high-low+1];
        int k = 0;//tmp数组的下标
        while(s1<=mid&&s2<=high){
            if(array[s1]<=array[s2]){
                /*tmp[k] = array[s1];
                k++;
                s1++;*/
                tmp[k++] = array[s1++];
            }
            if(array[s1]>=array[s2]){
                tmp[k++] = array[s2++];
            }
        }
        while(s1<=mid){
            tmp[k++] = array[s1++];
        }
        while(s2<=mid){
            tmp[k++] = array[s2++];
        }
        for(int i = 0;i<tmp.length;i++){
            array[i+low] = tmp[i];
        }
    }
    public static void mergeSort(int[] array){
        mergeSortInternal(array,0,array.length-1);
    }

    //非递归
    public static void merge2(int[] array,int gap){
        int s1 = 0;
        int e1 = s1+gap-1;
        int s2 = e1+1;
        int e2 = s2+gap-1<array.length ? s2+gap-1 : array.length-1;
        int[] tmp = new int[array.length];
        int k = 0;
        //当有两个归并段时
        while(s2<array.length){
            while(s1<=e1 && s2<=e2){//当有两个归并段且这两个归并段都有数据
                if(array[s1]<=array[s2]){
                    tmp[k++] = array[s1++];
                }else{
                    tmp[k++] = array[s2++];
                }
            }
            while(s1<=e1){
                tmp[k++] = array[s1++];
            }
            while(s2<=e2){
                tmp[k++] = array[s2++];
            }
            s1 = e2+1;
            e1 = s1+gap-1;
            s2 = e1+1;
            e2 = s2+gap-1<array.length ? s2+gap-1 : array.length-1;
        }
        while(s1<array.length){//退出上边循环后，将s1段内数据拷贝下来，因为有可能e1已经越界
            tmp[k++] = array[s1++];
        }
        for(int i = 0;i<tmp.length;i++){
            array[i] = tmp[i]; 
        }

    }
    public static void mergeSort2(int[] array){
        for (int i = 1; i < array.length; i*=2) {
            merge2(array,i);
        }
    }

    /**
     * 快速排序
     * 时间复杂度：O(nlogn) O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * 调JVM栈参数
     * @param array
     * @param start
     * @param end
     * @return
     */


    public static int pivot(int[] array,int start,int end) {
        int tmp = array[start];
        while (start < end) {
            while (start < end && array[end]>=tmp) {
                end--;
            }
            //把数据赋值给start
            array[start] = array[end];
            while (start < end && array[start]<=tmp) {
                start++;
            }
            //把start下标的值给end
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }
    public static void quick(int[] array,int low,int high){
        //if(low<high){
        if(low >= high) return;
        //进行直接插入法优化
        if(high-low+1<=50){
            insertSortBound(array, low, high);
            return;//一定要return，说明这个区间范围有序了，直接结束
        }
        //进行三数取中法优化
        medianOfThree(array,low,high);
        int piv = pivot(array,low,high);
        quick(array, low, piv-1);
        quick(array, piv+1, high);
        //}
    }

    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    public static void insertSortBound(int[] array,int low,int high){
        for (int i = low+1; i <= high; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= low ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void medianOfThree(int[] array,int low,int high){
        int mid = (low+high)/2;
        if(array[low]<=array[mid]){
            int tmp = array[low];
            array[low] = array[mid];
            array[mid] = tmp;
        }
        if(array[low]>=array[high]){
            int tmp = array[low];
            array[low] = array[high];
            array[high] = tmp;
        }
        if(array[mid]>array[high]){
            int tmp = array[mid];
            array[mid] = array[high];
            array[high] = tmp;
        }
    }
    //非递归

    public static void quickSort2(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int low = 0;
        int high = array.length-1;
        int piv = pivot(array,low,high);
        if(piv>low+1){
            stack.push(low);
            stack.push(piv-1);
        }
        if(piv<high-1){
            stack.push(piv+1);
            stack.push(high);
        }
        while(!stack.empty()){
            high = stack.pop();
            low = stack.pop();
            piv = pivot(array,low,high);
            if(piv>low+1){
                stack.push(low);
                stack.push(piv-1);
            }
            if(piv<high-1){
                stack.push(piv+1);
                stack.push(high);
            }
        }
    }

    /**
     * 选择排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */

    public static void selectSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i]>array[j]){
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：O(1.5) O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * @param array
     * @param gap
     */
    public static void shell(int[] array ,int gap) {
        for(int i = gap;i<array.length;i++){
            int tmp = array[i];
            int j = i-gap;
            for(;j>=0;j -= gap){
                if(array[j]>tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }



    public static void shellSort(int[] array) {
        int[] drr = {5,3,1};//增量数组

        for (int i = 0; i < drr.length; i++) {
            shell(array,drr[i]);
        }
    }

    /**
     * 插入排序
     * 时间复杂度：
     * 最坏情况下：当数据是无序的情况下：O(n^2)
     * 最好情况下：当数据有序的时候，可以达到O(n)
     *     所以：结论：越有序越快。
     *     题目：当前有一组待排序序列，但是这组待排序序列大部分是有序的。
     *         请问，下面那个排序更适合    直接插入排序。
     *     另外：直接插入排序一般也会用在一些排序的优化上。
     *          快速排序。
     * 空间复杂度：
     * 稳定性：稳定的排序
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0 ; j--) {
                //如果这里是一个大于等于号 此时这个排序就不稳定了
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] array = new int[1_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            //array[i] = i;
            array[i] = random.nextInt(1_0000);
        }
        long startTime = System.currentTimeMillis();
        mergeSort2(array);
        //quickSort(array);
        //quickSort2(array);
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
        System.out.println(Arrays.toString(array));
        /*System.out.println(Arrays.toString(array));
        insertSort(array);
        System.out.println(Arrays.toString(array));
        shellSort(array);
        System.out.println(Arrays.toString(array));
        selectSort(array);
        System.out.println(Arrays.toString(array));
        quickSort(array);
        System.out.println(Arrays.toString(array));*/
    }
}

